package com.rom.tree.threadedBinaryTree;

/**
 * @author YLNO
 * @version 1.0
 * @date 2023/12/13 13:45
 * 树的线索化
 */
public class ThreadedBinaryTree {
    public static void main(String[] args) {

        BinaryTree binaryTree = new BinaryTree();

        HeroNode heroNode1 = new HeroNode(1,"zhang");
        HeroNode heroNode2 = new HeroNode(3,"zhang");
        HeroNode heroNode3 = new HeroNode(6,"zhang");
        HeroNode heroNode4 = new HeroNode(8,"zhang");
        HeroNode heroNode5 = new HeroNode(10,"zhang");
        HeroNode heroNode6 = new HeroNode(14,"zhang");

        heroNode1.setLeftNode(heroNode2);
        heroNode1.setRightNode(heroNode3);
        heroNode3.setLeftNode(heroNode6);
        heroNode2.setLeftNode(heroNode4);
        heroNode2.setRightNode(heroNode5);

//        binaryTree.
//        binaryTree.infixOrder(heroNode1);
        System.out.println("------------------------");
        binaryTree.threadedNodes(heroNode1);

        //线索化排序
        System.out.println("------------------------》排序");
        binaryTree.threadList(heroNode1);
        //中序后继查找
        System.out.println("====》查找");
        System.out.println(heroNode4.getRightNode());
    }
}

class BinaryTree{

    private HeroNode root;

    private HeroNode pre;

    //中序遍历
    public void infixOrder(HeroNode root){
        if(root==null){
            System.out.println("不可遍历");
        }
        root.infixOrder();
    }
    //线索化遍历
    public void threadList(HeroNode node){

//        HeroNode node = root;
        while (node != null){

            while (node.getLeftType() == 0){
                node = node.getLeftNode();
            }
            System.out.println(node);
            while (node.getRightType() == 1){
                node = node.getRightNode();
                System.out.println(node);
            }
            node = node.getRightNode();
        }
    }
    //中序线索化
    public void threadedNodes(HeroNode node){

        if(node == null){
            return ;
//            System.out.println("不可以线索化");
        }
        //左子树
        threadedNodes(node.getLeftNode());

        if(node.getLeftNode()==null){
            node.setLeftNode(pre);
            node.setLeftType(1);
        }

        if(pre!=null && pre.getRightNode() == null){
            pre.setRightNode(node);
            pre.setRightType(1);
        }
        pre = node;
        //右子树
        threadedNodes(node.getRightNode());
    }
}

class HeroNode{

    private int no;
    private String name;
    private HeroNode leftNode;
    private HeroNode rightNode;

    //左右侧类型
    private int leftType;
    private int rightType;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(HeroNode leftNode) {
        this.leftNode = leftNode;
    }

    public HeroNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(HeroNode rightNode) {
        this.rightNode = rightNode;
    }

    //前序遍历
    public void preOrder(){

        System.out.println(this);
        if(this.leftNode!=null){
            this.leftNode.preOrder();
        }
        if(this.rightNode!=null){
            this.rightNode.preOrder();
        }
    }
    //前序查找
    public HeroNode preOrderSearch(int no){
        if(this.no == no){
            return this;
        }
        if(this.leftNode != null){
            HeroNode heroNode = this.leftNode.preOrderSearch(no);
            if(heroNode!=null){
                return heroNode;
            }
        }
        if(this.rightNode != null){
            HeroNode heroNode = this.rightNode.preOrderSearch(no);
            if(heroNode!=null){
                return heroNode;
            }
        }
        return null;
    }
    //前序遍历删除
    public void preOrderDelete(int no){


    }

    //中序遍历
    public void infixOrder(){

        if(this.leftNode!=null){
            this.leftNode.infixOrder();
        }
        System.out.println(this);
        if(this.rightNode!=null){
            this.rightNode.infixOrder();
        }
    }
    //中序查找
    public HeroNode infixOrderSearch(int no){

        return null;
    }
    //后序遍历
    public void postOrder(){

        if(this.leftNode!=null){
            this.leftNode.postOrder();
        }
        if(this.rightNode!=null){
            this.rightNode.postOrder();
        }
        System.out.println(this);
    }

    //后序查找
    public HeroNode postOrderSearch(int no){

        return null;
    }
}
